package com.leil.movies.project.service;

import com.alibaba.fastjson.JSONObject;
import com.leil.movies.project.properties.DouBanApiProperties;
import com.leil.movies.project.rest.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DouBanApiService {

    @Autowired
    DouBanApiProperties douBanApiProperties;

    @Autowired
    RestUtils restUtils;

    /**
     * 按类目搜索电影
     * @param sort      排序方式
     * @param tags      标签
     * @param start     从多少开始
     * @param genres    类目
     * @return          搜索结果
     */
    public JSONObject catSearch(String sort, String tags, int start, String genres) {
        // https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=&start=40&genres=剧情
        String url = this.douBanApiProperties.getCatSearchUrl() + "?sort=" + sort + "&range=0,10&tags=" + tags + "&start=" + start + "&genres=" + genres;
        JSONObject response = this.restUtils.doGet(url);
        return response;
    }


    /**
     * 关键字搜索
     * @param q     关键字
     * @return      搜索结果
     */
    public JSONObject subjectSearch(String q) {
        // https://movie.douban.com/j/subject_suggest?q=关键字
        String url = this.douBanApiProperties.getSearchUrl() + "?q=" + q;
        JSONObject response = this.restUtils.doGetJsonArr(url);
        return response;
    }


    /**
     * 获取电影条目
     */
    public JSONObject getSubject(String id) {
        // /v2/movie/subject/:id
        String url = this.douBanApiProperties.getSubjectUrl() + id;
        JSONObject response = this.restUtils.doGet(this.appendApikey(url));
        return response;
    }

    /**
     * 获取电影条目剧照
     */
    public JSONObject getSubjectPhotos(String id, int start, int count) {
        // /v2/movie/subject/:id/photos
        String url = this.douBanApiProperties.getSubjectUrl() + id + this.douBanApiProperties.getPhotos();
        String fUrl = this.appendApikey(url) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }

    /**
     * 获取电影条目评价列表
     */
    public JSONObject getSubjectReviews(String id, int start, int count) {
        // /v2/movie/subject/:id/reviews
        String url = this.douBanApiProperties.getSubjectUrl() + id + this.douBanApiProperties.getReviews();
        String fUrl = this.appendApikey(url) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }

    /**
     * 获取电影短评列表
     */
    public JSONObject getSubjectComments(String id, int start, int count) {
        // /v2/movie/subject/:id/comments
        String url = this.douBanApiProperties.getSubjectUrl() + id + this.douBanApiProperties.getComments();
        String fUrl = this.appendApikey(url) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }

    /**
     * 获取影人条目
     */
    public JSONObject getCelebrity(String id) {
        ///v2/movie/celebrity/:id
        String url = this.douBanApiProperties.getCelebtityUrl() + id;
        JSONObject response = this.restUtils.doGet(this.appendApikey(url));
        return response;
    }

    /**
     * 获取影人作品
     */
    public JSONObject getCelebrityWorks(String id, int start, int count) {
        // /v2/movie/celebrity/:id/works
        String url = this.douBanApiProperties.getCelebtityUrl() + id + this.douBanApiProperties.getWorks();
        String fUrl = this.appendApikey(url) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }

    /**
     * 获取影人剧照
     */
    public JSONObject getCelebrityPhotos(String id, int start, int count) {
        // /v2/movie/celebrity/:id/photos
        String url = this.douBanApiProperties.getCelebtityUrl() + id + this.douBanApiProperties.getPhotos();
        String fUrl = this.appendApikey(url) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }

    /**
     * TOP250
     */
    public JSONObject getTop250(int start, int count) {
        // /v2/movie/top250
        String fUrl = this.appendApikey(this.douBanApiProperties.getTopUrl()) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }


    /**
     * 北美票房榜
     */
    public JSONObject getUsBox() {
        // /v2/movie/us_box
        JSONObject response = this.restUtils.doGet(this.appendApikey(this.douBanApiProperties.getUsBoxUrl()));
        return response;
    }

    /**
     * 口碑榜
     */
    public JSONObject getWeekly() {
        // /v2/movie/weekly
        JSONObject response = this.restUtils.doGet(this.appendApikey(this.douBanApiProperties.getWeeklyUrl()));
        return response;
    }

    /**
     * 新片榜
     */
    public JSONObject getNewMovies() {
        // /v2/movie/new_movies
        JSONObject response = this.restUtils.doGet(this.appendApikey(this.douBanApiProperties.getNewMoviesUrl()));
        return response;
    }

    /**
     * 正在上映
     */
    public JSONObject getInTheaters() {
        // /v2/movie/in_theaters
        JSONObject response = this.restUtils.doGet(this.appendApikey(this.douBanApiProperties.getInTheatersUrl()));
        return response;
    }

    /**
     * 即将上映
     */
    public JSONObject getComingSoon(int start, int count) {
        // /v2/movie/coming_soon
        String fUrl = this.appendApikey(this.douBanApiProperties.getComingSoonUrl()) + "&start=" + start + "&count=" + count;
        JSONObject response = this.restUtils.doGet(fUrl);
        return response;
    }


    /**
     * 添加apikey
     * @param url   api url
     */
    private String appendApikey(String url) {
        return url + "?apikey=" + this.douBanApiProperties.getApikey();
    }



}
